import os, re

def pipy_url(type = None):      # 将不同的 pipy 转化，方便调用
    url = "pip install "
    return {
        None:url,
        "阿里云": url + "-i https://mirrors.aliyun.com/pypi/simple/ ",
        "豆瓣": url + "-i http://pypi.douban.com/simple/ ",
        "搜狐": url + "-i http://mirrors.sohu.com/python/ ",
        "清华": url + "-i https://pypi.tuna.tsinghua.edu.cn/simple ",
        "华中理工大学": url + "-i http://pypi.hustunique.com/simple/ ",
        "山东理工大学": url + "-i http://pypi.sdutlinux.org/simple/ ",
        "中国科学技术大学": url + "-i https://mirrors.ustc.edu.cn/pypi/ "
    }[type]

def pipy(name,type = None):        # 接受提供的数值，传入 popen
    url = pipy_url(type) + name
    return os_popen(url)

def os_popen(txt):                  # 发送os指令 popen 返回结果
    return os.popen(txt).read()

def list():                          # 获取当前已安装的库
    list = os_popen("pip list")
    return list.split()[4::2], list.split()[5::2]

def txt_print():                    # 将库的数组转化成文本形式输出
    txt = ""
    Packagen, Version = list()
    for i in range(len(Packagen)):
        txt = txt + str(Packagen[i]) + "【" + str(Version[i]) + "】" + "\r\n"
    return txt

def find_Version(txt):             # 查找指定库的版本号
    Packagen, Version = list()
    for i in range(len(Packagen)):
        if Packagen[i] == txt:
            return Version[i]
    return 0